home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / usr / sybase / sample / dblibrary / example7.c < prev    next >
C/C++ Source or Header  |  1993-04-22  |  5KB  |  231 lines

  1. /*
  2. **    example7.c
  3. **
  4. ** This example illustrates the use of browse-mode routines to
  5. ** determine the source of result columns from ad hoc queries.
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. #include <sybfront.h>
  11. #include <sybdb.h>
  12.  
  13. void          examine_results();
  14. void          send_command();
  15.  
  16. /* Forward declarations of the error-handler and message-handler routines. */
  17. int           err_handler();
  18. int           msg_handler();
  19.  
  20. main()
  21. {
  22.     LOGINREC              *login;
  23.     DBPROCESS             *dbproc;
  24.  
  25.     int                   command_count;
  26.     RETCODE               retcode;
  27.  
  28.     /* Initialize DB-Library. */
  29.     if (dbinit() == FAIL)
  30.         exit(ERREXIT);
  31.  
  32.     /* Install the user-supplied error-handling and message-handling
  33.      * routines. They are defined at the bottom of this source file.
  34.      */
  35.     dberrhandle(err_handler);
  36.     dbmsghandle(msg_handler);
  37.     
  38.     /* Allocate and initialize the LOGINREC structure to be used
  39.      * to open a connection to SQL Server.
  40.      */
  41.  
  42.     login = dblogin();
  43.     DBSETLPWD(login, "server_password");
  44.     DBSETLAPP(login, "example7");
  45.     
  46.     dbproc = dbopen(login, NULL);
  47.  
  48.     /* Allow the user to type in a series of queries. This program
  49.      * will be terminated by the word "quit", appearing at the
  50.      * beginning of the line.
  51.      */
  52.     while (1)
  53.     {
  54.         /* Send a user-generated query to SQL Server. */
  55.         send_command(dbproc);
  56.  
  57.         /* Now, examine the results of any queries the user has
  58.          * typed in.
  59.          */
  60.  
  61.         command_count = 1;
  62.         while ((retcode = dbresults(dbproc)) != NO_MORE_RESULTS)
  63.         {
  64.             if (retcode == FAIL)
  65.                 printf("Command %d failed.\n", command_count);
  66.             else
  67.             {
  68.                 if (!(DBCMDROW(dbproc)))
  69.                     printf
  70.                         ("Command %d returned no rows.\n",
  71.                          command_count);
  72.                 else
  73.                 {
  74.                     /* This is a command that can return
  75.                      * rows. Let's take a closer look at it.
  76.                      */
  77.                     printf("Command %d:\n", command_count);
  78.                     examine_results(dbproc);
  79.  
  80.                     /* Throw away all data rows. */
  81.                     dbcanquery(dbproc);
  82.                 }
  83.             }
  84.         }
  85.     }
  86. }
  87.  
  88.  
  89. void examine_results(dbproc)
  90. DBPROCESS        *dbproc;
  91. {
  92.     int            colcount;
  93.     int            colnum;
  94.     char           fullsource[128];
  95.     char           *sourcecolname;
  96.     int            tabcount;
  97.     char           *tabname;
  98.     int            tabnum;
  99.  
  100.     /* Determine which tables were used to generate the query results. */
  101.  
  102.     tabcount = dbtabcount(dbproc);
  103.     printf
  104.         ("The following tables were used to generate these query results:\n");
  105.  
  106.     for (tabnum = 1; tabnum <= tabcount; tabnum++)
  107.     {
  108.         if ((tabname = dbtabname(dbproc, tabnum)) != NULL)
  109.             printf
  110.                 ("\t%s (%s)\n", tabname,
  111.                  (dbtabbrowse(dbproc, tabnum)
  112.                   ? "browsable" : "not browsable"));
  113.     }
  114.  
  115.     /* Determine which tables were used to generate each result column. */
  116.  
  117.     colcount = dbnumcols(dbproc);
  118.     printf("Here are the columns of the target list and their sources:\n");
  119.  
  120.     printf
  121.         ("\t%-20s   %-30s   %s\n\n",
  122.          "Result column:", "Source:", "Browsable?");
  123.     for (colnum = 1; colnum <= colcount; colnum++)
  124.     {
  125.         tabname = dbtabsource(dbproc, colnum, NULL);
  126.         sourcecolname = dbcolsource(dbproc, colnum);
  127.  
  128.         if (tabname == NULL)
  129.             strcpy(fullsource, "(result of expression)");
  130.         else
  131.             sprintf(fullsource, "%s.%s", tabname, sourcecolname);
  132.  
  133.         printf
  134.             ("\t%-20s   %-30s   %s\n",
  135.              dbcolname(dbproc, colnum),
  136.              fullsource,
  137.              (dbcolbrowse(dbproc, colnum) ? "yes" : "no"));
  138.     }
  139. }
  140.  
  141.  
  142.  
  143. void send_command(dbproc)
  144. DBPROCESS        *dbproc;
  145. {
  146.     char           cmdbuf[2048];
  147.  
  148.     /* Allow the user to type in an ad hoc query. This query
  149.      * will be terminated by the word "go" appearing at the
  150.      * beginning of the line.
  151.      *
  152.      * If the user types the word "quit" at the beginning of a line,
  153.      * we'll quit the program.
  154.      */
  155.     printf("Enter a SQL query:\n");
  156.     while (1)
  157.     {
  158.         printf("> ");
  159.         gets(cmdbuf);
  160.  
  161.         if (strcmp(cmdbuf, "go") == 0)
  162.         {
  163.             dbsqlexec(dbproc);
  164.             break;
  165.         }
  166.         else if (strcmp(cmdbuf, "quit") == 0)
  167.         {
  168.  
  169.             dbexit();
  170.             exit(STDEXIT);
  171.         }
  172.         else
  173.         {
  174.             /* Keep reading SQL commands. */
  175.             dbcmd(dbproc, cmdbuf);
  176.         }
  177.     }
  178. }
  179.  
  180.  
  181.  
  182. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  183. DBPROCESS       *dbproc;
  184. int             severity;
  185. int             dberr;
  186. int             oserr;
  187. char            *dberrstr;
  188. char            *oserrstr;
  189. {
  190.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  191.         return(INT_EXIT);
  192.     else 
  193.     {
  194.         printf("DB-Library error:\n\t%s\n", dberrstr);
  195.  
  196.         if (oserr != DBNOERR)
  197.             printf("Operating-system error:\n\t%s\n", oserrstr);
  198.  
  199.         return(INT_CANCEL);
  200.     }
  201. }
  202.  
  203.  
  204. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  205.                 srvname, procname, line)
  206.  
  207. DBPROCESS       *dbproc;
  208. DBINT           msgno;
  209. int             msgstate;
  210. int             severity;
  211. char            *msgtext;
  212. char            *srvname;
  213. char            *procname;
  214. DBUSMALLINT     line;
  215.  
  216. {
  217.     printf ("Msg %ld, Level %d, State %d\n", 
  218.             msgno, severity, msgstate);
  219.  
  220.     if (strlen(srvname) > 0)
  221.         printf ("Server '%s', ", srvname);
  222.     if (strlen(procname) > 0)
  223.         printf ("Procedure '%s', ", procname);
  224.     if (line > 0)
  225.         printf ("Line %d", line);
  226.  
  227.     printf("\n\t%s\n", msgtext);
  228.  
  229.     return(0);
  230. }
  231.